﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Spinning.Spinning.Unitity;
using Spinning.Spinning.Interface;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using Spinning.Spinning.Entity;

namespace Spinning.Spinning.Entity
{
    class CircleGesture : BaseEntity, ITouchable
    {
        TextEntity te;
        public float BigRadius { get; set; }
        public float SmailRadius { get; set; }

        public double _startAngle;
        public double _firstTouchAngle;

        readonly TimeSpan deltaSpan = new TimeSpan(0, 0, 0, 0, 100);
        private DateTime _startTime;

        public delegate void TouchEventHandler(BaseEntity entity, TouchLocation touchLocation);
        public TouchEventHandler OnTouchEvent, OnReleaseEvent, OnHoldEvent;

        public delegate void RunCircleEventHandler();
        public RunCircleEventHandler OnRunCircleEvent;

        public delegate void AreaChangeEventHandler();
        public AreaChangeEventHandler OnAreaChangeEvent;
        public bool isPress;

        public bool IsStop { get; set; }

        public CircleGesture()
            : base()
        {
            te = new TextEntity();
            ResourceManager.ContentRoot = "";
            te.fontString = "TestFont";
            te.OnLoad();
            te.Origin = new Vector2(0.5f);
            te.Position = new Vector2(200, 200);
            te.Color = Color.Black;
            te.Alpha = 0;
            te.ZIndex = Constants.debugText;
            Attach(te);

            isPress = false;
            
            initList();
        }

        public bool Contain(Vector2 position) 
        {
            float distance;
            distance = (position.X - Position.X) * (position.X - Position.X) + (position.Y - Position.Y) * (position.Y - Position.Y);
            distance = (float)Math.Sqrt(distance);
            return (/*distance > SmailRadius &&*/ distance < BigRadius);
        }

        public void OnTouch(TouchLocation touchLocation) 
        {
            isPress = true;
            _startAngle = GetAngle(CovPoint(touchLocation));
            _firstTouchAngle = _startAngle;
            _startTime = DateTime.Now;
            if (OnTouchEvent != null)
                OnTouchEvent(this, touchLocation);
        }

        public double GetAngle(Point point) 
        {
            double angle;
            
            angle = (int)(Math.Atan2( point.Y-Position.Y,point.X-Position.X ) * 180 / Math.PI)+180;
            return angle;
        }

        public Point CovPoint(TouchLocation touchLocation) 
        {
            return new Point((int)touchLocation.Position.X, (int)touchLocation.Position.Y);
        }

        public void OnRelease(TouchLocation touchLocation) 
        {
            initList();
            isPress = false;
            if (OnReleaseEvent != null)
                OnReleaseEvent(this, touchLocation);
        }

        public List<bool> angleList;
        int currentArea, preArea;

        public int CurrentArea
        {
            get { return currentArea; }
            set
            {
                if (value == 3) currentArea = 0;
                else if (value == -1) currentArea = 2;
                else currentArea = value;

                if (OnAreaChangeEvent != null)
                    OnAreaChangeEvent();
                
            }
        }
        int PreArea
        {
            get { return preArea; }
            set
            {
                if (value == 3) preArea = 0;
                else if (value == -1) preArea = 2;
                else preArea = value;
            }
        }

        public void OnHold(TouchLocation touchLocation) 
        {
            DateTime now = DateTime.Now;

            if (now - _startTime > deltaSpan)
            {
                _startAngle = GetAngle(CovPoint(touchLocation));
                _startTime = DateTime.Now;
                CurrentArea = (int)(_startAngle / 120);
                int temPreArea1 = PreArea + 1;
                int temPreArea2 = PreArea - 1;
                if (temPreArea1 == CurrentArea) angleList[currentArea] = false;
                else if (temPreArea2 == CurrentArea) angleList[currentArea] = true;
                
                te.Scale = new Vector2(2);

                te.Value = "\nCurrentArea : " + CurrentArea + "\nPreArea : " + PreArea
                    + "\nCount:" + checkList();

                if (checkList()==2 && !IsStop)
                {
                    OnRunCircleEvent();
                    initList();
                }

                PreArea = CurrentArea;
            }
            
            if (OnHoldEvent != null)
                OnHoldEvent(this, touchLocation);
        }

        void initList() 
        {
            angleList = new List<bool>();
            for (int i = 0; i < 3; i++)
            {
                angleList.Add(false);
            }
        }

        int checkList()
        {
            int ans = 0;
            for (int i = 0; i < 3; i++)
            {
                if (angleList[i]) ans++;
            }
            return ans;
        }

    }
}
